package com.xja.dubbo.controller;

import com.xja.dubbo.entity.Easybuy_user;
import com.xja.dubbo.service.ProductCategoryService;
import com.xja.dubbo.service.ProductService;
import com.xja.dubbo.service.UserService;
import com.xja.dubbo.util.CookieUtil;
import com.xja.dubbo.util.UuidUtil;
import io.netty.handler.codec.http.HttpResponse;
import org.apache.dubbo.config.annotation.Reference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author ycz
 * @create 2021-12-18-9:29
 */
@Controller
@RequestMapping("")
public class UserController {

    private Logger logger = LoggerFactory.getLogger(IndexController.class);

    @Reference(url = "dubbo://localhost:20882")
    private UserService userService;

    @RequestMapping("login")
    public String login(Easybuy_user easybuy_user, HttpServletRequest request, HttpServletResponse response){
        try {
            logger.info("买家登录");
            Easybuy_user user = userService.findUserByName(easybuy_user.getLoginname());
            if(user==null || !user.getPassword().equals(easybuy_user.getPassword())){
                return "login";
            }

            String uuid = UuidUtil.getUUID();
            userService.saveLoginUserToRedis(user, uuid);

            //传到客户端cookie
            CookieUtil.setCookie(request, response, "uuid", uuid);

            return "redirect:/index";

        } catch (Exception e) {
            logger.error("登录失败", e);
        }
        return "redirect:error.html";

    }
}
